#ifndef SERVICES_H
#define SERVICES_H
#include "querypackage.h"
#include "api.h"
#include "user.h"
#include <QJsonObject>
class Services
{
public:
    Services() = delete;
    static void sendEmail(QString email,ApiCallback func = QueryFunc){
        Api::getInstance()->put(
            apipath_email,
            {
                {"email", email}
            },
            outtime,
            func
            );
    }
    static void sendQuery(QueryPackage* item,int page,int size,ApiCallback func = QueryFunc){
        QJsonObject json = item->toJson();
        json["page"] = QString::number(page);
        json["size"] = QString::number(size);
        Api::getInstance()->post(
            apipath_query+QueryPackage::TypetoString(item->qrytype),
            json,
            outtime,
            func
        );
    }
    static void sendSimpQuery(QueryPackage* item,ApiCallback func = QueryFunc){
        QJsonObject json = item->toJson();
        Api::getInstance()->post(
            apipath_query+QueryPackage::TypetoString(item->qrytype),
            json,
            outtime,
            func
            );
    }
    static void sendRegister(User* user,QString verifycode,ApiCallback func = QueryFunc){
        QJsonObject json = user->toJson();
        json["verifycode"]=verifycode;
        Api::getInstance()->post(
            apipath_register,
            json,
            outtime,
            func
            );
    }
    static void sendLoginRequest(QString usn,QString pwd,QString type,ApiCallback func){
        QJsonObject json = {
            {"Username",usn},
            {"Password",pwd},
            {"Type",type}
        };
        Api::getInstance()->post(
            "/api/v1/login",
            json,
            10000,
            func
            );
    }
    static void getUserList(QString id,ApiCallback func){
        QJsonObject json = {{"id",id}};
        Api::getInstance()->get(
            "/api/v1/sendFriendUser",
            json,
            5000,
            func
            );
    }
    static void getCity(ApiCallback func){
        Api::getInstance()->get(
            "/api/v1/cities",
            {},
            100000,
            func
            );
    }
    static void getStation(ApiCallback func){
        Api::getInstance()->get(
            "/api/v1/stations",
            {},
            100000,
            func
            );
    }
private:
    static const int outtime;
    static const QString apipath_email;
    static const QString apipath_query;
    static const QString apipath_register;
    static const ApiCallback QueryFunc;
    static void onQueryCallback(bool success, const QJsonObject& response, const QString& errorMsg, int StatusCode) {
        if (success) {
            qDebug() << "发送成功，响应：" << response;
        } else {
            qDebug() << "发送失败：" << errorMsg << "，状态码：" << StatusCode;
        }
    }
    //static void

};

#endif // SERVICES_H
